package com.epcs.controller.action;

import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;

import org.apache.struts2.dispatcher.SessionMap;
import org.apache.struts2.interceptor.ServletRequestAware;
import org.apache.struts2.interceptor.SessionAware;
import org.hibernate.metamodel.relational.Value;

import com.epcs.model.bo.CommentBO;
import com.epcs.model.bo.EventBO;
import com.epcs.model.bo.EventcategoryBO;
import com.epcs.model.bo.FollowingBO;
import com.epcs.model.bo.NotificationBO;
import com.epcs.model.dto.Account;
import com.epcs.model.dto.Comment;
import com.epcs.model.dto.Event;
import com.epcs.model.dto.Eventcategory;
import com.epcs.model.dto.Following;
import com.epcs.model.dto.Notification;
import com.epcs.util.EpcsDateTimeUtil;
import com.mysql.jdbc.NotUpdatable;
import com.opensymphony.xwork2.ActionSupport;

public class ManageNotificationAction extends ActionSupport implements SessionAware, ServletRequestAware{
	private String notificationId;
	private SessionMap<String, Object> sessionMap;
	private HttpServletRequest request;

	public String getAllNotification(){
		NotificationBO notificationBO = new NotificationBO();
		Account account = (Account) sessionMap.get("USER_INFO");
		List<Notification> notificationList = notificationBO.getAllNotificationForAccount(account);
		if(notificationList!=null){
			List<String> titleList = notificationBO.getTitlebyNotification(notificationList);
			request.setAttribute("TITLE_LIST", titleList);
			request.setAttribute("NOTIFICATION_LIST", notificationList);
		}else{
			request.setAttribute("IS_NULL", "true");
		}
		
	
		return "Success";
	}
	public String processedNotification(){
		NotificationBO notificationBO = new NotificationBO();
		EventBO newEventBO = new EventBO();
		FollowingBO followBO = new FollowingBO();
		Account account = (Account) sessionMap.get("USER_INFO");
		
		
		int id = Integer.valueOf(notificationId);
		Following followingOfNotification = notificationBO.getFollowingByNotificationId(id);
		String eventID = followingOfNotification.getIdOfFollow().toString();
		Event eventDetail = newEventBO.getEvent(eventID);
		
		if(eventDetail!= null){
			// dùng request để biết có phải là chủ sở hữu event k
			if(eventDetail.getOrganizer() == null && account.getUsername().equals(eventDetail.getAccount().getUsername())){
				request.setAttribute("IS_OWNER", "true");
			}
			Following following =  followBO.loadFollowingEvent(account,eventID);
			// dùng request để biết có phải là sự kiện public k
			if("public".equals(eventDetail.getVisibility()) || account.getUsername().equals(eventDetail.getAccount().getUsername()) || following!=null){
				request.setAttribute("IS_PUBLIC_EVENT", "true");
			}
			//Kiểm tra xem có phải event chính không
			if(eventDetail.getOrganizer() == null){
				request.setAttribute("IS_MAIN_EVENT", "true");
			}
			if("confirmed".equals(eventDetail.getStatus())){
				request.setAttribute("IS_CONFIRM_EVENT", "true");
			}
			
			eventDetail.setStartTime(EpcsDateTimeUtil.convertToJSPDatetime(eventDetail.getStartTime()));
			eventDetail.setEndTime((EpcsDateTimeUtil.convertToJSPDatetime(eventDetail.getEndTime())));

			// Lay commnet
			CommentBO newCommentBO = new CommentBO();
			List<Comment> commentList = newCommentBO.getAllCommentByEventId(eventDetail);
			// Lay event category

			EventcategoryBO newCatBO = new EventcategoryBO();
			request.setAttribute("EVENT_DETAIL", eventDetail);
			request.setAttribute("COMMENT_LIST", commentList);
			
			// ----------------------------------------Nhân------------------------------------------------------
			
			Following follow = followBO.loadFollowing(account, Integer.parseInt(eventID));

			request.setAttribute("FOLLOWING_EVENT", follow);
			// ---------------------------------------------------------------------------------------------------
			
			// ----------------------------------------Đăng------------------------------------------------------		
			request.setAttribute("NUMBER_OF_FOLLOWING", followBO.getNumberOfFollowingByEvent(eventDetail));
			// xét trạng thái hiện tại của event
			long currentTime = EpcsDateTimeUtil.getMillisecondsByString(EpcsDateTimeUtil.getCurrentIsoDateTime());
			long eventStartTime = EpcsDateTimeUtil.getMillisecondsByString(eventDetail.getStartTime());
			long eventStopTime = EpcsDateTimeUtil.getMillisecondsByString(eventDetail.getEndTime());
			
			if (eventStartTime <= currentTime && currentTime <= eventStopTime){
				request.setAttribute("CURRENT_EVENT_STATUS", "Đang diễn ra");
			} else if (currentTime < eventStartTime) {
				request.setAttribute("CURRENT_EVENT_STATUS", "Sắp diễn ra");
			} else {
				request.setAttribute("CURRENT_EVENT_STATUS", "Đã kết thúc");
			}
			// ---------------------------------------------------------------------------------------------------

			return "Success";
		}else{
			return "NotExit";
		}
	}
	
	public String getNotificationId() {
		return notificationId;
	}
	public void setNotificationId(String notificationId) {
		this.notificationId = notificationId;
	}
	//------------------------------------------------------------------------
	@Override
	public void setSession(Map<String, Object> sessionMap) {
		this.sessionMap = (SessionMap<String, Object>) sessionMap;
	}

	@Override
	public void setServletRequest(HttpServletRequest request) {
		this.request = request;

	}
}
